<template>
	<view class="container">
		<view class="publish-box">
			<view class="content-box">
				<view class="title-box"> 标题： </view>
				<view>
					<u--textarea v-model="title"></u--textarea>
				</view>
				<view class="title-box"> 内容： </view>
				<view>
					<u--textarea v-model="content"></u--textarea>
				</view>
			</view>
			<view class="content-box">
				<view class="title-box"> 发点什么图呢？ </view>
				<view>
					<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="10"
						:previewFullImage="true"></u-upload>
				</view>
			</view>
		</view>
		<view style="
        padding: 30rpx 100rpx;
        position: fixed;
        bottom: 50rpx;
        width: 100%;
        box-sizing: border-box;
      ">
			<u-button type="primary" color="#216be6" text="发布" @click="publishClick"></u-button>
		</view>
	</view>
</template>

<script>
	import baseUrl from "@/api/axios/baseUrl.js";
	import store from "@/store/index.js";
	import {
		save,
		info
	} from "@/api/axios/Space.js";
	export default {
		data() {
			return {
				content: "",
				title: "",
				fileList: [],
				openId: "",
				mapData: {},
				id: ''
			};
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				console.log(event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				console.log(this[`fileList${event.name}`]);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl.url + "/xihefile/user/file/upload",
						filePath: url,
						name: "file",
						formData: {},
						success: (uploadFileRes) => {
							console.log(uploadFileRes);
							console.log(uploadFileRes.data.data);
							let url = JSON.parse(uploadFileRes.data).data;
							console.log(url, "url");
							resolve(url);
						},
					});
				});
			},
			publishClick() {
				let _this = this
				let openId = uni.getStorageSync("openId");
				this.openId = openId;
				let url = this.fileList
					.map((item) => {
						return item.url;
					})
					.join(",");
				save({
					openId: openId,
					id: this.id ? this.id : '',
					title: this.title,
					content: this.content,
					picture: url,
					video: "",
					locationName: store.state.mapData.formatted_addresses ? store.state.mapData.formatted_addresses
						.standard_address : '',
					contentType: "3",
					isPrivate: "0",
				}).then(
					(res) => {
						console.log(res);
						_this.clue({
							action: '11',
							eventType: '1',
						})
						uni.navigateBack({
							delta: 1
						});
					},
					(reject) => {}
				);
			},
		},
		created() {},
		mounted() {},
		onLoad(data) {
			let _this = this
			this.id = data.id
			let openId = uni.getStorageSync("openId");
			if (data.edit) {
				info({
					params: {
						openId,
						id: data.id,
					},
				}).then(res => {
					_this.content = res.content;
					_this.title = res.title;
					_this.fileList = res.photoList.map(item => {
						return {
							url: item
						}
					});
				})


			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>